package com.db.compare.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import com.db.compare.exp.RbacUserExp;
import com.db.compare.model.RbacUser;
import com.db.compare.mapper.RbacUserMapper;
import com.db.compare.service.RbacUserService;
import com.db.compare.vo.RbacUserVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class RbacUserServiceImpl implements RbacUserService {
    @Autowired
    private RbacUserMapper rbacuserMapper;
    @Override
    public PageInfo<RbacUser> pageInfo(RbacUserVo rbacuserVo) {
        PageHelper.startPage(rbacuserVo.getPageNum(),rbacuserVo.getPageSize());
        List<RbacUser> rbacuserList = rbacuserMapper.findList(rbacuserVo);
        PageInfo<RbacUser> pageInfo = new PageInfo<>(rbacuserList);
        return pageInfo;
    }

    @Override
    public RbacUser findByUserPhone(String phone) {
        RbacUserVo rbacuserVo = new RbacUserVo();
        rbacuserVo.setMobile(phone);
        List<RbacUser> rbacuserList = rbacuserMapper.findList(rbacuserVo);
        if(rbacuserList!=null && !rbacuserList.isEmpty()){
            return rbacuserList.get(0);
        }
        return null;
    }

    //todo
    @Override
    public Set<String> findPermissions(String phone) {
        Set<String> permissions = new HashSet<>();
        permissions.add("sys:user:view");
        permissions.add("sys:user:add");
        permissions.add("sys:user:edit");
        permissions.add("sys:user:delete");
        permissions.add("index");
        return permissions;
    }

    @Override
    public void download(String ids, HttpServletResponse response) throws Exception {
        String fileName = "用户管理";
        try {
            ServletOutputStream outputStream=Appcontext.downloadResponse(response,fileName);
            if(StringUtils.isBlank(ids)){
                EasyExcel.write(outputStream, RbacUserExp.class).sheet(fileName).doWrite(null);
            }else{
                String[] arrays = ids.split(",");
                List<RbacUser> rbacuserList = rbacuserMapper.findByIds(arrays);
                List<RbacUserExp> rbacuserExpList = new ArrayList<>();
                for(RbacUser rbacuser:rbacuserList){
                    RbacUserExp rbacuserExp = new RbacUserExp();
                    BeanUtils.copyProperties(rbacuser,rbacuserExp);
                    rbacuserExpList.add(rbacuserExp);
                }
                EasyExcel.write(outputStream, RbacUserExp.class).sheet(fileName).doWrite(rbacuserExpList);
            }
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean upload(MultipartFile file) {
        ImportParams importParams = new ImportParams();
        try {
            ExcelImportResult<RbacUserExp> result = ExcelImportUtil.importExcelMore(file.getInputStream(), RbacUserExp.class,
                    importParams);
            List<RbacUserExp> rbacuserExps = result.getList();
            for (RbacUserExp rbacuserExp : rbacuserExps) {
                RbacUser rbacuser = new RbacUser();
                BeanUtils.copyProperties(rbacuser,rbacuserExp);
                int i = rbacuserMapper.insert(rbacuser);
                if(i < 1){
                    return false;
                }
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
